package com.exam.service.impl;

import com.exam.dao.ExamDao;
import com.exam.dao.QuestionDao;
import com.exam.domain.entity.Exam;
import com.exam.domain.entity.Question;
import com.exam.service.ExamService;
import com.exam.util.BaseResult;

import java.util.List;

public class ExamServiceImpl implements ExamService {

    private final ExamDao examDao;

    private final QuestionDao questionDao;

    public ExamServiceImpl() {
        examDao = new ExamDao();
        questionDao = new QuestionDao();
    }

    @Override
    public BaseResult<String> activeExamStatus(Integer examId) {
        // 查询试卷信息
        Exam exam = examDao.getExamInfoById(examId);
        if("active".equals(exam.getStatus())){
            return BaseResult.error("启动失败，试卷已启动！");
        }

        Integer result = examDao.activeExamStatusById(examId);

        return result>0?BaseResult.success("启动成功"):BaseResult.success("启动失败");
    }

    @Override
    public BaseResult<String> inactiveExamStatus(Integer examId) {
        Exam exam = examDao.getExamInfoById(examId);
        if("inactive".equals(exam.getStatus())){
            return BaseResult.error("停止失败，试卷已停止！");
        }
        Integer result = examDao.inactiveExamStatusById(examId);
        return result>0?BaseResult.success("停止成功"):BaseResult.success("停止失败");
    }



    // 查询相关数据
    @Override
    public BaseResult<Exam> findExamItemsByExamId(Integer examId) {
        Exam exam = examDao.getExamInfoById(examId);
        // 通过exam 继续查询试题信息
        List<Question> questions = questionDao.getQuestionListByExamId(examId);
        exam.setQuestions(questions);
        return BaseResult.success(exam);
    }

    @Override
    public BaseResult<Exam> getExamRecords(Integer studentId) {
        // 获得学生的考试记录

        return null;
    }
}
